package com.gufe.mapper.user.community;

import com.gufe.entity.PostComment;
import com.gufe.vo.communityvo.PostCommentVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface CommentMapper {
    // 获取帖子对应的评论数据
    @Select("SELECT pc.id, pc.post_id, pc.user_id, pc.content, pc.created_at,u.username, " +
            "u.avatar FROM post_comments pc LEFT JOIN users u ON pc.user_id = u.id " +
            "WHERE pc.post_id = #{postId} ORDER BY pc.created_at DESC")
    List<PostCommentVO> getComments(Integer postId);

    // 新增评论
    @Insert("insert into post_comments (post_id, user_id, content) values (#{postId},#{userId},#{content})")
    void addComment(PostComment postComment);

    @Update("UPDATE posts SET comment_count = comment_count + 1 WHERE id = #{postId}")
    void updatePostCommentCount(Integer postId);
}
